//
//  ViewController.m
//  21-NetWork
//
//  Created by 鹏飞罗 on 16/8/11.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *netWorkImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
}

// 同步方法
- (IBAction)syncConect:(id)sender {
    NSURL *imageURL = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"GET";
    
    NSURLResponse *response = nil;
    NSError *error = nil;
//    同步请求
//    参数1：请求Request
//    参数2； response指针的地址
//    参数3； error指针的地址
   NSData *data = [NSURLConnection sendSynchronousRequest:imageURLRequest returningResponse:&response error:&error];
    if(data) {
        _netWorkImage.image = [UIImage imageWithData:data];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"点击--");
    }


// 异步方法
- (IBAction)asyncConect:(id)sender {
    NSURL *imageURL = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest: imageURLRequest queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        _netWorkImage.image = [UIImage imageWithData:data];
    }];
}

- (IBAction)urlSessionAsync:(id)sender {
    
//    URLSession,网络请求
//    参数1：request
//    参数2： Block快， CallBack回调
//    注意： Session的回调处于异步线程， 需要返回主线程刷新UI（图片赋值， TableView CollectionView的刷新）
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *imageURL = [NSURL URLWithString:@"http://open3.bantangapp.com/base/app/init"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"POST";
    imageURLRequest.HTTPBody = [@"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17" dataUsingEncoding:NSUTF8StringEncoding];
    // URLSession, 网络请求
    // 参数1: request
    // 参数2: Block块, CallBack回调
    // 注意: Session的回调中处于异步线程, 需要返回主线程刷新UI(图片赋值, TableView CollectionView的刷新)
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 主线程执行图片加载
        dispatch_async(dispatch_get_main_queue(), ^{
            //            _networkImage.image = [UIImage imageWithData:data];
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@", result);
        });
    }];
    // 让 dataTask 执行
    [dataTask resume];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
